To do: Look at fort point and golden gate bridge locations
https://stackoverflow.com/questions/23130604/plot-coordinates-on-map https://stackoverflow.com/questions/15624656/label-points-in-geom-point https://stackoverflow.com/questions/38925225/how-to-add-labels-to-points-conditionally-in-a-city-map-using-ggmap https://ggrepel.slowkow.com/articles/examples.html
rm(list=ls())
# loading the required packages
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.0.5
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(mapproj)
## Warning: package 'mapproj' was built under R version 4.0.3
## Loading required package: maps
## Warning: package 'maps' was built under R version 4.0.3
library(ggrepel)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## v purrr 0.3.4
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x purrr::map() masks maps::map()
# read in data
sites<-read.csv("C:/Users/chels/Box Sync/Thesis/Data/Working data/GitHubReady/maps/site_locations.csv", header = TRUE, sep=",", fileEncoding="UTF-8-BOM", stringsAsFactors = FALSE)
# convert latitude to numeric
sites$latitude<-as.numeric(sites$latitude)
#not using code below, keeping for reference
# creating a sample data.frame with your lat/lon points
#lon <- sites$longitude
#lat <- sites$latitude
#df <- as.data.frame(cbind(lon,lat))
#API key from google
api <- readLines("C:/Users/chels/Box Sync/Thesis/google.api.txt") # Text file with the API key
register_google(key = api)
# getting the map
mapgilbert <- get_map(
location = c(lon= -122.48, lat = 37.9),
zoom = 11,
maptype = "satellite",
scale = 2,
source = c("google")
)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=37.9,-122.48&zoom=11&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx
Exploring different options
ggmap(mapgilbert) +
geom_point(
data = sites,
aes(
x = longitude,
y = latitude,
fill = type
),
size = 2,
shape = 21
) +
theme_void()
Need the sites labeled
ggmap(mapgilbert,
base_layer = ggplot(sites,
aes(x = longitude, y = latitude, fill = type))) +
geom_point() +
geom_label_repel(aes(label = name),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'black')
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
I’d rather have the points colored
ggmap(mapgilbert,
base_layer = ggplot(
sites,
aes(x = longitude, y = latitude, fill = type),
size = 2,
shape = 21
)) +
geom_point() +
geom_text(aes(label = name),
vjust = 0,
hjust = 0)
ggmap(mapgilbert,
base_layer = ggplot(
sites,
aes(x = longitude, y = latitude)
)) +
geom_point(aes(fill=sites$type),
size = 2,
shape = 21) +
geom_text(aes(label = name),
vjust = 0,
hjust = 0)
## Warning: Use of `sites$type` is discouraged. Use `type` instead.
ggmap(mapgilbert,
base_layer = ggplot(
sites,
aes(x = longitude, y = latitude)
)) +
geom_point(aes(fill=sites$type),
size = 2,
shape = 21) +
geom_label_repel(aes(label = name),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'black')
## Warning: Use of `sites$type` is discouraged. Use `type` instead.
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
ggmap(mapgilbert,
base_layer = ggplot(
sites,
aes(x = longitude, y = latitude)
)) +
geom_point(aes(fill=sites$type),
size = 2,
shape = 21) +
geom_label_repel(aes(label=name),
size=3,
label.size = NA,
label.padding=.2,
na.rm=TRUE,
fill = alpha(c("white"),0.5),
box.padding = 0.35,
point.padding = 0.5,)
## Warning: Use of `sites$type` is discouraged. Use `type` instead.
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
ggmap(mapgilbert,
base_layer = ggplot(
sites,
aes(x = longitude, y = latitude)
)) +
geom_point(aes(fill=sites$type),
size = 2,
shape = 21) +
geom_label_repel(aes(label=name),
size=3,
label.size = NA,
label.padding=.2,
na.rm=TRUE,
fill = alpha(c("white"),0.5),
box.padding = 0.35,
point.padding = 0.5,) +
labs(title="Study Area",
fill="Site Type")
## Warning: Use of `sites$type` is discouraged. Use `type` instead.
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf